﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using model;
using System.Data;
using System.Data.SqlClient;

namespace dao
{
    public class LienheDao:BaseDao
    {
        public List<LienheModel> getAllLienhe()
        {
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.CommandText = "sp_Lienhe_SelectAll";
            sqlCmd.Parameters.Clear();
            sqlCmd.Connection.Close();
            sqlCmd.Connection.Open();

            using (SqlDataReader dr = sqlCmd.ExecuteReader())
            {
                List<LienheModel> glstLienhe = new List<LienheModel>();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        LienheModel bv = new LienheModel();
                        bv.ID = Convert.ToInt64(dr["ID"]);
                        bv.Hoten = Convert.ToString(dr["hoten"]);
                        bv.Email = Convert.ToString(dr["email"]);
                        bv.Sodienthoai = Convert.ToString(dr["sodienthoai"]);
                        bv.Noidung = Convert.ToString(dr["noidung"]);
                        bv.Tieude = Convert.ToString(dr["tieude"]);
                        bv.IsBVThicong = Convert.ToString(dr["isBVThicong"]);
                        bv.Diachi = Convert.ToString(dr["diachi"]);
                        bv.Trangthai = Convert.ToString(dr["trangthai"]);
                        glstLienhe.Add(bv);
                    }
                    dr.Close();
                    sqlCmd.Connection.Close();
                }
                return glstLienhe;
            }
        }

        public string themmoiLienhe(LienheModel bv)
        {
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.CommandText = "sp_Lienhe_Insert";
            sqlCmd.Parameters.Clear();

            sqlCmd.Parameters.Add("@hoten", SqlDbType.NVarChar).Value = bv.Hoten;
            sqlCmd.Parameters.Add("@email", SqlDbType.NVarChar).Value = bv.Email;
            sqlCmd.Parameters.Add("@sodienthoai", SqlDbType.NVarChar).Value = bv.Sodienthoai;
            sqlCmd.Parameters.Add("@tieude", SqlDbType.NVarChar).Value = bv.Tieude;
            sqlCmd.Parameters.Add("@noidung", SqlDbType.NVarChar).Value = bv.Noidung;
            sqlCmd.Parameters.Add("@trangthai", SqlDbType.NVarChar).Value = bv.Trangthai;
            sqlCmd.Parameters.Add("@isBVThicong", SqlDbType.NVarChar).Value = bv.IsBVThicong;
            sqlCmd.Parameters.Add("@diachi", SqlDbType.NVarChar).Value = bv.Diachi; 
            sqlCmd.Connection.Open();
            sqlCmd.ExecuteNonQuery();
            sqlCmd.Connection.Close();

            return "";
        }
    }
}
